﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DrSoftsFrame.Servers;
using DrSoftsFrame;

namespace PreeRegisterDAL
{
    public class OrderDAL
    {
        public static DataSet SelectPree(string OrderId, string iWorkPlanID,bool bPreTime)
        {
            //if (!PreeRegisterDAL.LHL.AvailableCountDAL.bDataBase)
            //{
            //    return AvailableCountDAL.GetData("Pree.xml");
            //}
            SqlCommand cmd = new SqlCommand();
            string strSql = @"declare @nMoney money,
@sql nvarchar(1200)

if exists(select * from OPDSystemParameter where cParameterCode='cParameterNumber')
select @nMoney=cParameterNumber from OPDSystemParameter where cParameterCode='cParameterNumber'
else
set @nMoney=0.5

set @sql=
'select a.iWorkPlanId WorkId,isnull(a.iWorkPlanDetailId,0) iDetailId,a.cPreeCode OrderId,a.iRegisteNum iNum,
0 OrderFee,CONVERT(varchar(100),a.dStartTime,23) OrderDate,a.dStartTime,a.dEndTime,a.dCreateDate TransDate,
(isnull(c.nDiagnoseRate,0)+ isnull(e.naddrate,0)+'+convert(varchar(20),@nMoney)+') Price
from OPDRegisterPree a with(nolock)  
join OPDWorkPlan b with(nolock)  on a.iWorkPlanId=b.iWorkPlanId
JOIN opddictdept e with(nolock)  ON a.ideptid = e.ideptid  
join OPDDiagnoseType c with(nolock)  on b.cDiagnoseTypeCode=c.cDiagnoseTypeCode
where a.cPreeCode='''+@OrderId+''' and a.iWorkPlanId='+CONVERT(VARCHAR(20),@iWorkPlanID)+';";
            if (bPreTime)
            {
                strSql += @"select ISNULL(max(a.iWorkPlanID),'+CONVERT(VARCHAR(20),@iWorkPlanID)+') WorkId ,ISNULL(max(a.nMaxPreengageNum),0) OrderCount,
ISNULL(max(a.nMaxPreengageNum),0)-ISNULL(sum(case when b.bCheck=1 or b.bPreRegister=1 or b.bLock=1 then 1 else 0 end),0) AvailableCount
from opdworkplan a with(nolock)  join OPDWorkPlanDetail b with(nolock)  on a.iWorkPlanID=b.iWorkPlanID
where a.iWorkPlanID='+CONVERT(VARCHAR(20),@iWorkPlanID)+';'";
            }
            else
            {
                strSql += @"select iWorkPlanID,isnull(nMaxPreengageNum,0) OrderCount ,(isnull(nMaxPreengageNum,0)-(select COUNT(*) c 
from OPDRegisterPree  with(nolock)  where iworkplanid='+CONVERT(VARCHAR(20),@iWorkPlanID)+' and bRegisteFlag in (0,1)
group by iWorkPlanId) -
(select isnull(COUNT(*),0) c from opdregister with(nolock)  where iworkplanid='+CONVERT(VARCHAR(20),@iWorkPlanID)+')) AvailableCount from OPDWorkPlan with(nolock)  where iWorkPlanID ='+CONVERT(VARCHAR(20),@iWorkPlanID)+';'";
            }
            strSql += @"exec sp_executesql @sql";
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iWorkPlanID", System.Data.SqlDbType.Int));
            cmd.Parameters["@iWorkPlanID"].Value = iWorkPlanID;
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@OrderId", System.Data.SqlDbType.VarChar, 20));
            cmd.Parameters["@OrderId"].Value = OrderId;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strSql;
            ClinicDb mydb = new ClinicDb();
            DataSet ds = mydb.ExecuteSqlCmdFillDs(cmd);
            //AvailableCountDAL.SaveXML("Pree.xml", ds.GetXml());
            return ds;
        }


        public static DataSet SelectPree_new(string OrderId, string iWorkPlanID, bool bPreTime)
        {
            //if (!PreeRegisterDAL.LHL.AvailableCountDAL.bDataBase)
            //{
            //    return AvailableCountDAL.GetData("Pree.xml");
            //}
            SqlCommand cmd = new SqlCommand();
            string strSql = @"declare @nMoney money

if exists(select * from OPDSystemParameter where cParameterCode='cParameterNumber')
select @nMoney=cParameterNumber from OPDSystemParameter where cParameterCode='cParameterNumber'
else
set @nMoney=0.5

select a.iWorkPlanId WorkId,isnull(a.iWorkPlanDetailId,0) iDetailId,a.cPreeCode OrderId,a.iRegisteNum iNum,
0 OrderFee,CONVERT(varchar(100),a.dStartTime,23) OrderDate,a.dStartTime,a.dEndTime,a.dCreateDate TransDate,
(isnull(c.nDiagnoseRate,0)+ isnull(e.naddrate,0)+@nMoney) Price,ordercode,iRegisterPreeNo as iPreRegister
from OPDRegisterPree a with(nolock)  
join OPDWorkPlan b with(nolock)  on a.iWorkPlanId=b.iWorkPlanId
JOIN opddictdept e with(nolock)  ON a.ideptid = e.ideptid  
join OPDDiagnoseType c with(nolock)  on b.cDiagnoseTypeCode=c.cDiagnoseTypeCode
where a.cPreeCode='{0}' and a.iWorkPlanId={1};";
            if (bPreTime)
            {
                strSql += @"select ISNULL(max(a.iWorkPlanID),{1}) WorkId ,ISNULL(max(a.nMaxPreengageNum),0) OrderCount,
ISNULL(max(a.nMaxPreengageNum),0)-ISNULL(sum(case when b.bCheck=1 or b.bPreRegister=1 or b.bLock=1 then 1 else 0 end),0) AvailableCount
from opdworkplan a with(nolock)  join OPDWorkPlanDetail b with(nolock)  on a.iWorkPlanID=b.iWorkPlanID
where a.iWorkPlanID={1}";
            }
            else
            {
                strSql += @"select iWorkPlanID,isnull(nMaxPreengageNum,0) OrderCount ,
(
isnull(nMaxPreengageNum,0)-
(select COUNT(*) c 
from OPDRegisterPree  with(nolock)  
where iworkplanid={1} and bRegisteFlag in (0,1)
group by iWorkPlanId
) -
(select isnull(COUNT(*),0) c 
from opdregister with(nolock)  
where iworkplanid={1}
)
) AvailableCount 
from OPDWorkPlan with(nolock)  
where iWorkPlanID ={1}";
            }            
            ClinicDb mydb = new ClinicDb();
            DataSet ds;
            //DataSet ds = mydb.ExecuteSqlCmdFillDs(cmd);  
            strSql = string.Format(strSql, OrderId, iWorkPlanID);
            ds = mydb.ExecuteSelect(strSql);
            //AvailableCountDAL.SaveXML("Pree.xml", ds.GetXml());
            return ds;
        }

        /// <summary>
        /// 新增同煤版本查询预约信息  liujia150812
        /// </summary>
        /// <param name="OrderId"></param>
        /// <param name="iWorkPlanID"></param>
        /// <param name="bPreTime"></param>
        /// <returns></returns>
        public static DataSet SelectPree_Tmkyy(string OrderId, string iWorkPlanID, bool bPreTime)
        {
            SqlCommand cmd = new SqlCommand();
            string strSql = @"declare @nMoney money,
@sql nvarchar(1200)

if exists(select * from OPDSystemParameter where cParameterCode='cParameterNumber')
select @nMoney=cParameterNumber from OPDSystemParameter where cParameterCode='cParameterNumber'
else
set @nMoney=0.5

set @sql=
'select a.iWorkPlanId WorkId,isnull(a.iWorkPlanDetailId,0) iDetailId,a.cPreRegisterCode OrderId,a.iPreNum iNum,
0 OrderFee,CONVERT(varchar(100),a.dStartdate,23) OrderDate,a.dStartdate dStartTime,a.dEnddate dEndTime,a.dregisterdate TransDate,
(isnull(c.nDiagnoseRate,0)+ isnull(e.naddrate,0)+'+convert(varchar(20),@nMoney)+') Price,ordercode,iPreRegister
from hmpreregister a with(nolock)  
join OPDWorkPlan b with(nolock)  on a.iWorkPlanId=b.iWorkPlanId
JOIN opddictdept e with(nolock)  ON a.ideptid = e.ideptid  
join OPDDiagnoseType c with(nolock)  on b.cDiagnoseTypeCode=c.cDiagnoseTypeCode
where a.cPreRegisterCode='''+@OrderId+''' and a.iWorkPlanId='+CONVERT(VARCHAR(20),@iWorkPlanID)+';";

            strSql += @"select ISNULL(max(a.iWorkPlanID),'+CONVERT(VARCHAR(20),@iWorkPlanID)+') WorkId ,ISNULL(max(a.nMaxPreengageNum),0) OrderCount,
ISNULL(max(a.nMaxPreengageNum),0)-ISNULL(sum(case when b.bclinicflag=1 or b.bdiagflag=1 or b.buseflag=1 then 1 else 0 end),0) AvailableCount
from opdworkplan a with(nolock)  join OPDWorkPlanDetail b with(nolock)  on a.iWorkPlanID=b.iWorkPlanID
where a.iWorkPlanID='+CONVERT(VARCHAR(20),@iWorkPlanID)+';'";


            strSql += @"exec sp_executesql @sql";
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iWorkPlanID", System.Data.SqlDbType.Int));
            cmd.Parameters["@iWorkPlanID"].Value = iWorkPlanID;
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@OrderId", System.Data.SqlDbType.VarChar, 20));
            cmd.Parameters["@OrderId"].Value = OrderId;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strSql;
            ClinicDb mydb = new ClinicDb();
            DataSet ds = mydb.ExecuteSqlCmdFillDs(cmd);
            //AvailableCountDAL.SaveXML("Pree.xml", ds.GetXml());
            return ds;
        }


        /// <summary>
        /// 患者预约验证 131020lhl
        /// </summary>
        /// <param name="cPatientName"></param>
        /// <param name="iSex"></param>
        /// <param name="dBirthday"></param>
        /// <param name="iInBlackCount"></param>
        /// <param name="cPatientCode"></param>
        /// <param name="cIdentityCard"></param>
        /// <param name="iWorkPlanId"></param>
        /// <param name="nTel"></param>
        /// <param name="dStartTime"></param>
        /// <param name="bFreePree"></param>
        /// <returns></returns>
        public static int VerifyPatientInfo(string ProcedureName, string cPatientName, int iSex, DateTime dBirthday, ref int iInBlackCount, string cPatientCode, string cIdentityCard, int iWorkPlanId, string nTel, DateTime dStartTime, int bFreePree, ref string ls_err)
        {
            //if (!PreeRegisterDAL.LHL.AvailableCountDAL.bDataBase)
            //{
            //    return true;
            //}
            ClinicDb mydb = new ClinicDb();
            try
            {
                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = ProcedureName;
                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter p1 = new SqlParameter("@cPatientName", SqlDbType.VarChar, 20);
                p1.Direction = ParameterDirection.Input;
                p1.Value = cPatientName;
                cmd.Parameters.Add(p1);

                SqlParameter p2 = new SqlParameter("@iSex", SqlDbType.Int);
                p2.Direction = ParameterDirection.Input;
                p2.Value = iSex;
                cmd.Parameters.Add(p2);

                SqlParameter p3 = new SqlParameter("@dBirthday", SqlDbType.DateTime);
                p3.Direction = ParameterDirection.Input;
                p3.Value = dBirthday;
                cmd.Parameters.Add(p3);


                SqlParameter p4 = new SqlParameter("@nTel", SqlDbType.VarChar, 20);
                p4.Direction = ParameterDirection.Input;
                p4.Value = nTel;
                cmd.Parameters.Add(p4);

                SqlParameter p5 = new SqlParameter("@cPatientCode", SqlDbType.VarChar, 30);
                p5.Direction = ParameterDirection.Input;
                p5.Value = cPatientCode;
                cmd.Parameters.Add(p5);


                SqlParameter p6 = new SqlParameter("@cIdentityCard", SqlDbType.VarChar, 30);
                p6.Direction = ParameterDirection.Input;
                p6.Value = cIdentityCard;
                cmd.Parameters.Add(p6);


                SqlParameter p7 = new SqlParameter("@iWorkPlanId", SqlDbType.Int);
                p7.Direction = ParameterDirection.Input;
                p7.Value = iWorkPlanId;
                cmd.Parameters.Add(p7);

                SqlParameter p8 = new SqlParameter("@dStartTime", SqlDbType.DateTime);
                p8.Direction = ParameterDirection.Input;
                p8.Value = dStartTime;
                cmd.Parameters.Add(p8);

                SqlParameter p9 = new SqlParameter("@bFreePree", SqlDbType.Int);
                p9.Direction = ParameterDirection.Input;
                p9.Value = bFreePree;
                cmd.Parameters.Add(p9);

                SqlParameter p10 = new SqlParameter("@iInBlackCount", SqlDbType.Int);
                p10.Direction = ParameterDirection.Output;
                p10.Value = iInBlackCount;
                cmd.Parameters.Add(p10);

                SqlParameter p11 = new SqlParameter("@ls_err", SqlDbType.VarChar, 50);
                p11.Direction = ParameterDirection.Output;
                p11.Value = ls_err;
                cmd.Parameters.Add(p11);

                SqlParameter p13 = new SqlParameter("@iOPID", SqlDbType.Int);
                p13.Direction = ParameterDirection.Input;
                p13.Value = -999;
                cmd.Parameters.Add(p13);

                SqlParameter p12 = new SqlParameter();
                p12.Direction = ParameterDirection.ReturnValue;
                cmd.Parameters.Add(p12);

                object[] ob = mydb.ExecuteSqlCmdWithOutParmList(cmd);
                iInBlackCount = Common.ToInt(ob[0]);
                ls_err = ob[1].ToString();

                //修改返回参数类型，改为返回值  liujia151201
                return (int)ob[2];
                //if ((int)ob[2] < 0)
                //    return false;
                //else
                //    return true;
            }
            catch (Exception err)
            {
                throw err;
            }
        }

        public static bool SavePreeInfo(string cPatientName, string cPreeType, string cPreeCode, string cTelCode, string cIdentityCard, string OPID,
            string iWorkPlanId, string StartTime, string EndTime, string iNum, string iSex, string iPatientType, string dBirthday, string cAddress, string cRatureType, string proName
            , string cPatientCode, ref string myerr, ref string return_cPreeCode, string iPreeType, string cOrderCode)
        {
            //if (!PreeRegisterDAL.LHL.AvailableCountDAL.bDataBase)
            //{
            //    return true;
            //}
            ClinicDb mydb = new ClinicDb();
            try
            {
                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = proName;
                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter p1 = new SqlParameter("@cPatientName", SqlDbType.VarChar, 20);
                p1.Direction = ParameterDirection.Input;
                p1.Value = cPatientName;
                cmd.Parameters.Add(p1);

                SqlParameter p2 = new SqlParameter("@cPreeType", SqlDbType.VarChar, 10);
                p2.Direction = ParameterDirection.Input;
                p2.Value = cPreeType;
                cmd.Parameters.Add(p2);

                SqlParameter p3 = new SqlParameter("@cPreeCode", SqlDbType.VarChar, 20);
                p3.Direction = ParameterDirection.Input;
                p3.Value = cPreeCode;
                cmd.Parameters.Add(p3);


                SqlParameter p4 = new SqlParameter("@cTelCode", SqlDbType.VarChar, 11);
                p4.Direction = ParameterDirection.Input;
                p4.Value = cTelCode;
                cmd.Parameters.Add(p4);

                SqlParameter p5 = new SqlParameter("@cIdentityCard", SqlDbType.VarChar, 20);
                p5.Direction = ParameterDirection.Input;
                p5.Value = cIdentityCard;
                cmd.Parameters.Add(p5);

                SqlParameter p6 = new SqlParameter("@iPreeOp", SqlDbType.Int);
                p6.Direction = ParameterDirection.Input;
                p6.Value = OPID;
                cmd.Parameters.Add(p6);

                SqlParameter p7 = new SqlParameter("@iWorkPlanId", SqlDbType.Int);
                p7.Direction = ParameterDirection.Input;
                p7.Value = iWorkPlanId;
                cmd.Parameters.Add(p7);

                SqlParameter p8 = new SqlParameter("@dStartTime", SqlDbType.DateTime);
                p8.Direction = ParameterDirection.Input;
                p8.Value = StartTime;
                cmd.Parameters.Add(p8);

                SqlParameter p9 = new SqlParameter("@dEndTime", SqlDbType.DateTime);
                p9.Direction = ParameterDirection.Input;
                p9.Value = EndTime;
                cmd.Parameters.Add(p9);

                SqlParameter p10 = new SqlParameter("@iRegisteNum", SqlDbType.Int);
                p10.Direction = ParameterDirection.Input;
                p10.Value = iNum;
                cmd.Parameters.Add(p10);

                SqlParameter p12 = new SqlParameter("@iSex", SqlDbType.Int);
                p12.Direction = ParameterDirection.Input;
                p12.Value = iSex;
                cmd.Parameters.Add(p12);

                SqlParameter p13 = new SqlParameter("@iPatientType", SqlDbType.Int);
                p13.Direction = ParameterDirection.Input;
                p13.Value = iPatientType;
                cmd.Parameters.Add(p13);

                SqlParameter p14 = new SqlParameter("@dBirthday", SqlDbType.DateTime);
                p14.Direction = ParameterDirection.Input;
                p14.Value = dBirthday;
                cmd.Parameters.Add(p14);

                SqlParameter p15 = new SqlParameter("@cAddress", SqlDbType.VarChar, 50);
                p15.Direction = ParameterDirection.Input;
                p15.Value = cAddress;
                cmd.Parameters.Add(p15);


                string errMessage = "";
                SqlParameter p11 = new SqlParameter("@errMessage", SqlDbType.VarChar, 20);
                p11.Direction = ParameterDirection.Output;
                p11.Value = errMessage;
                cmd.Parameters.Add(p11);

                SqlParameter p16 = new SqlParameter("@cRatureType", SqlDbType.VarChar, 20);
                p16.Direction = ParameterDirection.Input;
                p16.Value = cRatureType;
                cmd.Parameters.Add(p16);

                SqlParameter p17 = new SqlParameter();
                p17.Direction = ParameterDirection.ReturnValue;
                cmd.Parameters.Add(p17);

                SqlParameter p18 = new SqlParameter("@cPatientCode", SqlDbType.VarChar, 20);
                p18.Direction = ParameterDirection.Input;
                p18.Value = cPatientCode;
                cmd.Parameters.Add(p18);

                SqlParameter p19 = new SqlParameter("@return_cPreeCode", SqlDbType.VarChar, 20);
                p19.Direction = ParameterDirection.Output;
                p19.Value = return_cPreeCode;
                cmd.Parameters.Add(p19);

                SqlParameter p20 = new SqlParameter("@iPreeType", SqlDbType.Int);
                p20.Direction = ParameterDirection.Input;
                p20.Value = iPreeType;
                cmd.Parameters.Add(p20);

                SqlParameter p21 = new SqlParameter("@orderCode", SqlDbType.VarChar,50);
                p21.Direction = ParameterDirection.Input;
                p21.Value = cOrderCode;
                cmd.Parameters.Add(p21);

                object[] o = mydb.ExecuteSqlCmdWithOutParmList(cmd);

                if (o[0].ToString() != "")
                {
                    mydb.Dispose();
                    myerr = o[0].ToString();
                    throw new Exception(o[0].ToString());
                }

                if (o[1].ToString() != "")
                {
                    return_cPreeCode = o[1].ToString();
                }
                
                return true;
            }
            catch (Exception err)
            {
                throw err;
            }
        }

        /// <summary>
        /// 增加同煤版标识  liujia150812
        /// </summary>
        /// <param name="IFTmkyy"></param>
        /// <param name="cPreeCode"></param>
        /// <param name="iWorkPlanID"></param>
        /// <param name="iDetailID"></param>
        /// <param name="bPreTime"></param>
        public static void DeleteData(bool IFTmkyy,string cPreeCode, string iWorkPlanID, string iDetailID, bool bPreTime)
        {
            //if (!PreeRegisterDAL.LHL.AvailableCountDAL.bDataBase)
            //{
            //    return;
            //}
            SqlCommand cmd = new SqlCommand();
            // 增加同煤版标识  liujia150812
            string strSql = "";
            if (IFTmkyy)
            {
                strSql = @"delete from hmpreregister where iWorkPlanId=@iWorkPlanID and cPreRegisterCode=@cPreeCode and isnull(bimportFlag,0)=0;";
            }
            else
            {
                strSql = @"delete from OPDRegisterPree where iWorkPlanId=@iWorkPlanID and cPreeCode=@cPreeCode and isnull(bRegisteFlag,0)=0;";
            }
            if (bPreTime)
                strSql += @"update OPDWorkPlanDetail set bLock=0 where   iDetailId=@iDetailID;
            update OPDWorkPlan set npreengageNum=npreengageNum-1 where   iWorkPlanId=@iWorkPlanID;";
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iDetailID", System.Data.SqlDbType.Int));
            cmd.Parameters["@iDetailID"].Value = iDetailID;
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iWorkPlanID", System.Data.SqlDbType.Int));
            cmd.Parameters["@iWorkPlanID"].Value = iWorkPlanID;
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cPreeCode", System.Data.SqlDbType.VarChar, 10));
            cmd.Parameters["@cPreeCode"].Value = cPreeCode;
            cmd.CommandType = CommandType.Text;//删除预约数据修改为cmd方式  lhl20131215
            cmd.CommandText = strSql;
            ClinicDb mydb = new ClinicDb();
            mydb.ExecuteSqlCmdFillDs(cmd);
        }

        public static DataSet SelectNum(string iWorkPlanID)
        {
            //if (!PreeRegisterDAL.LHL.AvailableCountDAL.bDataBase)
            //{
            //    return AvailableCountDAL.GetData("PreeNum.xml");
            //}
            SqlCommand cmd = new SqlCommand();
            string strSql = @"select iWorkPlanId,isnull(preenbegin,0)+1 iNum,dStartTime from OPDWorkPlan with(nolock)  where   iWorkPlanId=@iWorkPlanID;";
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iWorkPlanID", System.Data.SqlDbType.Int));
            cmd.Parameters["@iWorkPlanID"].Value = iWorkPlanID;
            cmd.CommandType = CommandType.Text;//删除预约数据修改为cmd方式  lhl20131215
            cmd.CommandText = strSql;
            ClinicDb mydb = new ClinicDb();
            DataSet ds = mydb.ExecuteSqlCmdFillDs(cmd);
            //AvailableCountDAL.SaveXML("PreeNum.xml", ds.GetXml());
            return ds;
        }

        /// <summary>
        /// 获取订单号
        /// </summary>
        /// <param name="areaCode"></param>
        /// <param name="hosCode"></param>
        /// <param name="orderType"></param>
        /// <returns></returns>
        public static string GetNewOrderCode(string areaCode, string hosCode,string orderType)
        {

            ClinicDb mydb = new ClinicDb();
            try
            {
                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = "up_getNewOrderCode";
                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter p1 = new SqlParameter("@areaCode", SqlDbType.VarChar, 5);
                p1.Direction = ParameterDirection.Input;
                p1.Value = areaCode;
                cmd.Parameters.Add(p1);

                SqlParameter p2 = new SqlParameter("@hosCode", SqlDbType.VarChar, 10);
                p2.Direction = ParameterDirection.Input;
                p2.Value = hosCode;
                cmd.Parameters.Add(p2);

                SqlParameter p3 = new SqlParameter("@orderType", SqlDbType.VarChar, 5);
                p3.Direction = ParameterDirection.Input;
                p3.Value = orderType;
                cmd.Parameters.Add(p3);

                DataSet ds = mydb.ExecuteSqlCmdFillDs(cmd);

                if (ds == null || ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 1)
                {
                    return null;
                }

                return ds.Tables[0].Rows[0]["orderCode"].ToString();
            }
            catch (Exception err)
            {
                throw err;
            }
        }

        /// <summary>
        /// 查询排班时间  liujia151201
        /// </summary>
        /// <param name="iWorkPlanID"></param>
        /// <returns></returns>
        public static DataSet IFPreeRegister(int iWorkPlanID)
        {
            SqlCommand cmd = new SqlCommand();
            string strSql = @"select dStartTime,dEndTime
from OPDWorkPlan
where iWorkPlanID=@iWorkPlanID;";
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iWorkPlanID", System.Data.SqlDbType.Int));
            cmd.Parameters["@iWorkPlanID"].Value = iWorkPlanID;
            cmd.CommandType = CommandType.Text;//
            cmd.CommandText = strSql;
            ClinicDb mydb = new ClinicDb();
            DataSet ds = mydb.ExecuteSqlCmdFillDs(cmd);
            return ds;
        }
    }
}
